﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using KTool2.Utility;

namespace KTool2
{
    /// <summary>
    /// Interaction logic for TextChangeTimeWindow.xaml
    /// </summary>
    public partial class TextChangeTimeWindow : Window
    {
        private volatile static TextChangeTimeWindow instance = null;
        //public AppSetting setting;

        private TextChangeTimeWindow()
        {
            InitializeComponent();
            //this.setting = setting;
            this.txtTime.Text = AppSetting.Instance.MeanTime.Seconds.ToString();
            this.txtTime.SelectAll();
            this.txtTime.Focus();
        }

        public static TextChangeTimeWindow Instance
        {
            get
            {
                if (instance == null)
                {
                    System.Threading.Mutex mutex = new System.Threading.Mutex();
                    mutex.WaitOne();

                    if (instance == null)
                    {
                        instance = new TextChangeTimeWindow();
                    }

                    mutex.Close();
                }

                return instance;
            }
        }

        public static bool IsNull
        {
            get { return instance == null; }
        }

        private void btnSetTime_Click(object sender, RoutedEventArgs e)
        {
            AppSetting.Instance.MeanTime = TimeSpan.FromSeconds(double.Parse(this.txtTime.Text));
            this.Close();
        }

        protected override void OnClosed(EventArgs e)
        {
            instance = null;
            base.OnClosed(e);
        }

        private void txtTime_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            foreach (char item in e.Text)
            {
                if (!char.IsDigit(item))
                {
                    e.Handled = true;
                    break;
                }
            }
        }
    }
}
